package com.wise.eventserver.utils;

import com.google.common.hash.Hashing;
import com.outsider.enums.DataTypeEnum;
import com.outsider.enums.UtilEnum;
import com.outsider.factory.ClassFactory;
import com.outsider.reflect.IRequestData;
import com.outsider.strongdata.IObject;
import com.outsider.util.ILogger;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

@Component("cacheKeyGenerator")
class CacheKeyGenerator<T> implements KeyGenerator {
    // custom cache key
    public static final int NO_PARAM_KEY = 0;
    public static final int NULL_PARAM_KEY = 53;

    @Resource
    private IRequestData requestData;

    private ILogger logger = ClassFactory.requestUtil(UtilEnum.SLF4J);

    /*

        key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");
        if (params.length == 0) {
            return key.append(NO_PARAM_KEY).toString();
        }
        for (Object param : params) {
            if (param == null) {
                logger.debug("input null param for Spring cache, use default key={}");
                key.append(NULL_PARAM_KEY);
            } else if (ClassUtils.isPrimitiveArray(param.getClass())) {
                int length = Array.getLength(param);
                for (int i = 0; i < length; i++) {
                    key.append(Array.get(param, i));
                    key.append(',');
                }
            } else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {
                key.append(param);
            } else {
                logger.debug("Using an object as a cache key may lead to unexpected results. " +
                        "Either use @Cacheable(key=..) or implement CacheKey. Method is " + target.getClass() + "#" + method.getName());
                key.append(param.hashCode());
            }
            key.append('-');
        }

        String finalKey = key.toString();
        long cacheKeyHash = Hashing.murmur3_128().hashString(finalKey, Charset.defaultCharset()).asLong();
        logger.debug("using cache key={} hashCode={}" + finalKey);
    */

    @Override
    public Object generate(Object target, Method method, Object... params) {

        logger.setClass(getClass());
        StringBuilder key = new StringBuilder();

        try {
            String clsName = "";
            for (Object param : params) {
                if (param == null) {
                    logger.debug("input null param for Spring cache, use default key={}");
                    key.append(NULL_PARAM_KEY);
                } else {
                    clsName = param.getClass().getSimpleName();
                    key.append(clsName).append(":");

                    IObject object = requestData.newInstance(DataTypeEnum.OBJECT);
                    object.fromObject(param);
                    key.append(object.getPropertyValue("id").toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return key.toString();
    }
}
